package com.XiGuaXiaoMe.dao;

import com.XiGuaXiaoMe.entity.Users;
import com.XiGuaXiaoMe.util.JdbcUtil;

import java.awt.desktop.PreferencesHandler;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class userDao {
    private JdbcUtil util=new JdbcUtil();

   //用户注册
    public int add(Users user)
    {
        String sql="insert into users(userName,userName3,password,email)"+
                " values(?,?,?,?)";
        PreparedStatement ps=util.createStatement(sql);
        int result=0;

        try {
            ps.setString(1,user.getUserName());
            ps.setString(2,user.getUserName3());
            ps.setString(3,user.getPassword());
            ps.setString(4,user.getEmail());
            result= ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            util.close();
        }
        return result;
    }
    //查询所有用户信息
    public List findAll()
    {
        String sql="select * from users";
        PreparedStatement ps=util.createStatement(sql);
        ResultSet rs=null;
         List userList=new ArrayList();
        try {
            rs=ps.executeQuery();
            while(rs.next())
            {

                String userName=rs.getString("userName");
                String userName3=rs.getString("userName3");
                String passward=rs.getString("password");
                String email=rs.getString("email");
                Users users=new Users(userName,userName3,passward,email);
                userList.add(users);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            util.close(rs);
        }
        return userList;
    }
    //查询所有用户信息
    public Users find(String userName3,String userName)
    {

        String sql="select * from users where userName3=? and userName=?";
        PreparedStatement ps=util.createStatement(sql);
        ResultSet rs=null;
        Users user=new Users();
        try {
            ps.setString(1,userName3);
            ps.setString(2,userName);
            rs=ps.executeQuery();

            while(rs.next())
            {
                String username=rs.getString("userName");

                String username3=rs.getString("userName3");
                String passward=rs.getString("password");
                String email=rs.getString("email");
                System.out.println(username);
                System.out.println(username3);
                System.out.println(passward);
                System.out.println(email);

                 user=new Users(username,username3,passward,email);
                 if(user!=null)
                 {
                     System.out.println("success!!!");
                 }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            util.close(rs);
        }
        return user;
    }
    //根据用户编号删除用户信息
    public int delete(String userId)
    {
        String sql="delete from users where userId=?";
        PreparedStatement ps=util.createStatement(sql);
        int result=0;
        try {
            ps.setInt(1,Integer.valueOf(userId));
            result=ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            util.close();
        }
        return result;
    }
    //登录验证
    public int login(String userName3,String password)
    {
        String sql="select count(*) from users where userName3=? and password=?";
        PreparedStatement ps=util.createStatement(sql);
        ResultSet rs=null;
        int result=0;
        try {
            ps.setString(1,userName3);
            ps.setString(2,password);
            rs=ps.executeQuery();
            while(rs.next())
            {
                result=rs.getInt("count(*)");
                System.out.println(result+"in userDao");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            util.close(rs);
        }
        return result;
    }
    /*
     * 修改某个学生信息
     * */
    public int updateStu(Users user){
        int rs = 0;
        try{
            String sql = "update users set userName=?,password=?,email=? where userName3=?;";//修改一条学生信息
            PreparedStatement pst=util.createStatement(sql);
            pst.setString(1, user.getUserName());
            pst.setString(2, user.getPassword());
            pst.setString(3, user.getEmail());
            pst.setString(4, user.getUserName3());


            rs = pst.executeUpdate();
            if(rs!=0){
                System.out.println("Student_id"+user.getUserName3()+"update over!");
            }
        }catch(Exception e){e.printStackTrace();}
        return rs;
    }


}
